/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.spider.video.service.impl;

import me.zhengjie.spider.common.SpiderConstant;
import me.zhengjie.spider.util.SpiderUtil;
import me.zhengjie.spider.video.domain.SpiderVideo;
import me.zhengjie.utils.ValidationUtil;
import me.zhengjie.utils.FileUtil;
import lombok.RequiredArgsConstructor;
import me.zhengjie.spider.video.repository.SpiderVideoRepository;
import me.zhengjie.spider.video.service.SpiderVideoService;
import me.zhengjie.spider.video.service.dto.SpiderVideoDto;
import me.zhengjie.spider.video.service.dto.SpiderVideoQueryCriteria;
import me.zhengjie.spider.video.service.mapstruct.SpiderVideoMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import me.zhengjie.utils.PageUtil;
import me.zhengjie.utils.QueryHelp;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import me.zhengjie.utils.PageResult;

/**
* @website https://eladmin.vip
* @description 服务实现
* @author 雨来
* @date 2023-07-07
**/
@Service
@RequiredArgsConstructor
public class SpiderVideoServiceImpl implements SpiderVideoService {

    private final SpiderVideoRepository spiderVideoRepository;
    private final SpiderVideoMapper spiderVideoMapper;

    @Override
    public PageResult<SpiderVideoDto> queryAll(SpiderVideoQueryCriteria criteria, Pageable pageable){
        Page<SpiderVideo> page = spiderVideoRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(spiderVideoMapper::toDto));
    }

    @Override
    public List<SpiderVideoDto> queryAll(SpiderVideoQueryCriteria criteria){
        return spiderVideoMapper.toDto(spiderVideoRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    public List<String> queryAllByWebId(Long webId) {
        return spiderVideoRepository.findHrefByWebId(webId);
    }

    @Override
    public List<String> queryHrefAll() {
        return spiderVideoRepository.findHrefAll();
    }

    @Override
    public List<SpiderVideoDto> findUnSpiderByWebIdAnd(Long webId) {
        List<SpiderVideo> allByWebIdAndPlayLinkIsNull = spiderVideoRepository.findUnSpiderByWebIdAnd(webId);
        return spiderVideoMapper.toDto(allByWebIdAndPlayLinkIsNull);
    }

    @Override
    @Transactional
    public SpiderVideoDto findById(Long videoId) {
        SpiderVideo spiderVideo = spiderVideoRepository.findById(videoId).orElseGet(SpiderVideo::new);
        ValidationUtil.isNull(spiderVideo.getVideoId(),"SpiderVideo","videoId",videoId);
        return spiderVideoMapper.toDto(spiderVideo);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void create(SpiderVideo resources) {
        resources.setDeletedFlag(false);
        SpiderVideo save = spiderVideoRepository.save(resources);
        save.setLocalPath(String.format(SpiderConstant.FILE_FOLDER_VIDEO, SpiderUtil.getFolderNameByNum(save.getVideoId())));
        spiderVideoRepository.save(save);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(SpiderVideo resources) {
        SpiderVideo spiderVideo = spiderVideoRepository.findById(resources.getVideoId()).orElseGet(SpiderVideo::new);
        ValidationUtil.isNull( spiderVideo.getVideoId(),"SpiderVideo","id",resources.getVideoId());
        spiderVideo.copy(resources);
        spiderVideoRepository.save(spiderVideo);
    }

    @Override
    public void deleteAll(Long[] ids) {
        for (Long videoId : ids) {
            spiderVideoRepository.deleteById(videoId);
        }
    }

    @Override
    public void download(List<SpiderVideoDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (SpiderVideoDto spiderVideo : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("网站名称", spiderVideo.getWebId());
            map.put("视频名字", spiderVideo.getAlt());
            map.put("封面图", spiderVideo.getImg());
            map.put("详情地址", spiderVideo.getHref());
            map.put("分区", spiderVideo.getType());
            map.put("标签", spiderVideo.getTag());
            map.put("播放链接", spiderVideo.getPlayLink());
            map.put("本地链接", spiderVideo.getLocalPath());
            map.put("删除状态", spiderVideo.getDeletedFlag());
            map.put("备注", spiderVideo.getRemark());
            map.put("更新时间", spiderVideo.getUpdateTime());
            map.put("创建时间", spiderVideo.getCreateTime());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}